package com.blrs.controller;

import java.util.List;

import com.blrs.domain.dto.ProcessTemplateDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.blrs.common.core.controller.BaseController;
import com.blrs.common.core.domain.AjaxResult;
import com.blrs.common.core.page.TableDataInfo;
import com.blrs.domain.ProcessTemplate;
import com.blrs.service.process.IProcessTemplateService;

@RestController
@RequestMapping("/process/template")
public class ProcessTemplateController extends BaseController {
    @Autowired
    private IProcessTemplateService processTemplateService;

    @GetMapping("/list")
    public TableDataInfo list(ProcessTemplate processTemplate) {
        startPage();
        List<ProcessTemplate> list = processTemplateService.selectProcessTemplateList(processTemplate);
        return getDataTable(list);
    }

    @GetMapping("/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id) {
        return success(processTemplateService.selectProcessTemplateById(id));
    }

    @PostMapping
    public AjaxResult add(@RequestBody ProcessTemplateDTO templateDTO) {
        return toAjax(processTemplateService.insertProcessTemplate(templateDTO));
    }

    @PutMapping
    public AjaxResult edit(@RequestBody ProcessTemplateDTO templateDTO) {
        return toAjax(processTemplateService.updateProcessTemplate(templateDTO));
    }

    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids) {
        return toAjax(processTemplateService.deleteProcessTemplateByIds(ids));
    }

    /**
     * 校验模板名称是否唯一
     */
    @GetMapping("/checkTemplateNameUnique")
    public AjaxResult checkTemplateNameUnique(
            @RequestParam String templateName,
            @RequestParam(required = false) String id) {
        ProcessTemplate template = new ProcessTemplate();
        template.setTemplateName(templateName);
        template.setId(id);
        Integer count = processTemplateService.checkTemplateNameUnique(template);
        return AjaxResult.success(count == 0);
    }

} 